﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using DIWAGIS.Class;
using DIWAGIS.Modules;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;

using DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc;

namespace DIWAGIS.ApplicationLayer.TimKiemTaiSan
{
    public partial class frmTimKiemOngChinh : Form
    {
        public IFeature pFeature;
        Boolean isLoadComBox = false;
        string sTableName = generalVariable.database + "." + generalVariable.schema + "." + ClassVariables.wOngChinh;
        int sNResuilt = 0;
        Boolean kt = false;
        ILayer m_LayerOPP;
        IMapControl4 m_MapControl4;
        IHookHelper m_hookHelper = null;
        public frmTimKiemOngChinh()
        {
            InitializeComponent();
        }

        private void frmTimKiemOngPhanPhoi_Load(object sender, EventArgs e)
        {
            DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
            cbx.loadDataDomainToCombobox(cbxHieu, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMHieuOngNuoc");
            cbx.loadDataDomainToCombobox(cbxTinhTrang, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTinhTrangOngNuoc");
            cbx.loadDataDomainToCombobox(cbxVatLieu, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMChatLieuOngChinh");

            cbx.loadDataDomainToCombobox(cbxLoaiOng, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMLoaiOngNuoc");
            cbx.loadDataDomainToCombobox(cbxCapOng, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMCapOng");
            cbx.LoadSubTypeToCombobox(cbxCoOng, sTableName, "CoOng", DIWAGIS.Modules.generalVariable.ICurrentWorkspace);
            SqlCommand sqlcommand = new SqlCommand();
            sqlcommand.CommandText = "SELECT TENHUYEN,TENHUYEN FROM HUYEN ";
            cbx.loadDataToCombobox(cbxHuyen, sqlcommand);

            sqlcommand.CommandText = "SELECT TENCONDUONG,TENCONDUONG FROM " + generalVariable.database + "." + generalVariable.schema + "." + " CONDUONG ";
            cbx.loadDataToCombobox(cbxMaDuong, sqlcommand);
            isLoadComBox = true;
        }

        private void buttonTimKiem_Click(object sender, EventArgs e)
        {
            IFeatureWorkspace pFWS = DIWAGIS.Modules.generalVariable.ICurrentWorkspace as IFeatureWorkspace;

            string strWhere = "";


            if (txtMaTuyenOng.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND MaTuyenOng ='" + (txtMaTuyenOng.Text) + "'";
                }
                else
                {
                    strWhere = "MaTuyenOng ='" + (txtMaTuyenOng.Text) + "'";
                }
            }

            if (txtTenTuyen.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND TenTuyen =N'" + (txtTenTuyen.Text) + "'";
                }
                else
                {
                    strWhere = "TenTuyen =N'" + (txtMaTuyenOng.Text) + "'";
                }
            }

            if (cbxHieu.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND Hieu = " + (cbxHieu.SelectedValue);
                }
                else
                {
                    strWhere = "Hieu = " + (cbxHieu.SelectedValue);
                }
            }

            if (cbxVatLieu.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND ChatLieuOngChinh ='" + (cbxVatLieu.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "ChatLieuOngChinh ='" + (cbxVatLieu.SelectedValue) + "'";
                }
            }

            if (cbxTinhTrang.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND TinhTrang = " + (cbxTinhTrang.SelectedValue);
                }
                else
                {
                    strWhere = "TinhTrang = " + (cbxTinhTrang.SelectedValue);
                }
            }

            if (txtNamLapDat.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND NamLapDat = " + (txtNamLapDat.Text);
                }
                else
                {
                    strWhere = "NamLapDat = " + (txtNamLapDat.Text);
                }
            }

            if (cbxCapOng.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND CapOng = " + (cbxCapOng.SelectedValue);
                }
                else
                {
                    strWhere = "CapOng = " + (cbxCapOng.SelectedValue);
                }
            }

            if (cbxLoaiOng.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND LoaiOngNuoc = " + (cbxLoaiOng.SelectedValue);
                }
                else
                {
                    strWhere = "LoaiOngNuoc = " + (cbxLoaiOng.SelectedValue);
                }
            }


            if (cbxCoOng.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND CoOng = " + (cbxCoOng.SelectedValue);
                }
                else
                {
                    strWhere = "CoOng = " + (cbxCoOng.SelectedValue);
                }
            }

            if (cbxMaDuong.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND TenDuong =N'" + (cbxMaDuong.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "TenDuong =N'" + (cbxMaDuong.SelectedValue) + "'";
                }
            }

            if (cbxHuyen.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND Huyen = N'" + (cbxHuyen.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "Huyen =N'" + (cbxHuyen.SelectedValue) + "'";
                }
            }

            if (cbxPhuong.Text != string.Empty)
            {
                if (strWhere.ToString() != string.Empty)
                {
                    //strWhere = strWhere & " AND  (CoDongHo= " & Int16.Parse(cbxCoDongHo.SelectedValue) & ")"
                    strWhere = strWhere + " AND Phuong =N'" + (cbxPhuong.SelectedValue) + "'";
                }
                else
                {
                    strWhere = "Phuong =N'" + (cbxPhuong.SelectedValue) + "'";
                }
            }

            // ITable pTable = pFWS.OpenTable("gis_water.DBO.fcDongHoKhachHang") as ITable;

            IQueryDef queryDef = pFWS.CreateQueryDef();

            queryDef.Tables = sTableName;

            queryDef.SubFields = "MaTuyenOng,TenTuyen,MaDMA,Hieu,ChatLieuOngChinh,TinhTrang,NamLapDat,CoOng,CapOng,LoaiOngNuoc,TenDuong,Huyen,Phuong,ChieuDai";

            // Assign a where clause to filter the results.
            queryDef.WhereClause = strWhere;
            DIWAGIS.Class.WorkingWithTable pC = new DIWAGIS.Class.WorkingWithTable();
            ITable pTable = pC.CreateQueryTable(DIWAGIS.Modules.generalVariable.ICurrentWorkspace, queryDef, sTableName);

            sNResuilt = pTable.RowCount(queryDef as IQueryFilter);
            if (sNResuilt > 0)
            {
                lb_message.Text = "Có " + (sNResuilt.ToString()) + " đối tượng thỏa mãn tiêu chí tìm kiếm ";
            }
            dataGridView1.Columns.Clear();
            TableWrapper m_tableWrapper = new TableWrapper(pTable);
            m_tableWrapper.UseCVDomains = true;
            //m_tableWrapper. = "IDDanhBo,LoaiCauTao,NguyenLyHD,NgayLapDat,SoNha,IDTuyenDuong,IDKVCN,IDPhuongXa";
            //bindingSourceDongHoKhachHang.DataSource = m_tableWrapper;
            //dataGridView1.Columns.Clear();
            dataGridView1.DataSource = m_tableWrapper;// bindingSourceDongHoKhachHang;   
        }

        #region *ICommand Members

        public int Bitmap
        {
            get { throw new NotImplementedException(); }
        }

        public string Caption
        {
            get { throw new NotImplementedException(); }
        }

        public string Category
        {
            get { throw new NotImplementedException(); }
        }

        public bool Checked
        {
            get { throw new NotImplementedException(); }
        }

        public int HelpContextID
        {
            get { throw new NotImplementedException(); }
        }

        public string HelpFile
        {
            get { throw new NotImplementedException(); }
        }

        public string Message
        {
            get { throw new NotImplementedException(); }
        }

        public void OnClick()
        {

            //ICompositeLayer m_ComLyr = m_MapControl4.get_Layer(0) as ICompositeLayer;
            ILayer layer = null;
            for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++)
            {
                layer = m_hookHelper.FocusMap.get_Layer(i);
                if (layer.Name == "Ống chính")
                {
                    m_LayerOPP = layer;
                    break;
                }
            }
            this.Show();
        }

        public void OnCreate(object Hook)
        {
            m_hookHelper = new HookHelperClass();
            m_hookHelper.Hook = Hook;

        }
        public string Tooltip
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentRow.Cells[0].Value != null)
            {
                string st = "MaTuyenOng='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                DIWAGIS.Modules.generalVariable.pHookHelper = m_hookHelper;
                DIWAGIS.Class.general_Model f = new DIWAGIS.Class.general_Model();
                IFeature pfeat = f.findFeatureInTable(m_LayerOPP, st);


                IEnvelope m_envelope = pfeat.Extent;
                m_envelope.YMin -= 200;
                m_envelope.YMax += 200;
                m_envelope.XMax += 200;
                m_envelope.XMin -= 200;

                DIWAGIS.Modules.generalVariable.pHookHelper.ActiveView.Extent = m_envelope;
                DIWAGIS.Modules.generalVariable.pHookHelper.ActiveView.Refresh();
                frmOngChinh frm;
                frm = new frmOngChinh();
                frm.pFeature = pfeat;
                frm.ShowDialog();
            }
        }

        private void cbxHuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadComBox == true)
            {
                string strWhere = "";
                if (!string.IsNullOrEmpty(cbxHuyen.Text))
                {
                    strWhere = cbxHuyen.SelectedValue.ToString();
                    SqlCommand sqlcommand = new SqlCommand();
                    DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
                    //sqlcommand = new System.Data.SqlClient.SqlCommand();
                    sqlcommand.CommandText = "SELECT TenHanhChinh,TenHanhChinh FROM " + generalVariable.database + "." + generalVariable.schema + "." + "HANHCHINH" + " WHERE TenHuyen = @TenHuyen";
                    sqlcommand.Parameters.AddWithValue("@TenHuyen", strWhere);
                    cbx.loadDataToCombobox(cbxPhuong, sqlcommand);
                    //loadDataToCombobox(cbxMaPhuong, sqlcommand);

                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonXuatBaoCao_Click(object sender, EventArgs e)
        {

        }

        
    }
}
